﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsOfVirginia.MyEncoder
{
    internal class Encoder
    {
            /// <summary>
            /// 明文
            /// </summary>
            public string PlainText { get; set; }

            /// <summary>
            /// 秘钥
            /// </summary>
            public string KeyText { get; set; }

            /// <summary>
            /// 密文
            /// </summary>
            public string CypherText { get; set; }

            /// <summary>
            /// 加密动作
            /// </summary>
            public void Encode()
            {
                //加密过程
                //i为PlainText中对应的第i个字母，j遍历KeyText中所有的字母
                for (int i = 0, j = 0; i < PlainText.Length; i++, j++)
                {
                    if (j % KeyText.Length == 0)
                        j = 0;
                    int number = (CharToNo(PlainText[i]) + CharToNo(KeyText[j])) % 26;
                    CypherText += Convert.ToChar(number + 'a');
                }
            }

            /// <summary>
            /// 返回指定字符在字母表中对应的数字（从0开始）
            /// </summary>
            /// <param name="ch"></param>
            /// <returns></returns>
            private int CharToNo(char ch)
            {
                return ch - 'a';
            }      
    }
}
